package com.powerbank.financecenter.controller;

import com.powerbank.common.dto.AccountDTO;
import com.powerbank.common.dto.TransactionRequest;
import com.powerbank.common.dto.WithdrawalRequest;
import com.powerbank.common.result.Result;
import com.powerbank.financecenter.service.UserAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户账户控制器
 */
@Tag(name = "账户管理", description = "用户账户相关接口")
@RestController
@RequestMapping("/api/accounts")
@RequiredArgsConstructor
public class UserAccountController {

    private final UserAccountService userAccountService;

    @Operation(summary = "创建用户账户", description = "为用户创建指定类型的账户")
    @PostMapping
    public Result<AccountDTO> createAccount(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "账户类型") @RequestParam String accountType) {
        return userAccountService.createAccount(userId, accountType);
    }

    @Operation(summary = "查询账户详情", description = "根据用户ID和账户类型查询账户详情")
    @GetMapping
    public Result<AccountDTO> getAccountByUserIdAndType(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "账户类型") @RequestParam String accountType) {
        return userAccountService.getAccountByUserIdAndType(userId, accountType);
    }

    @Operation(summary = "查询用户所有账户", description = "查询指定用户的所有账户")
    @GetMapping("/user/{userId}")
    public Result<List<AccountDTO>> getUserAccounts(@Parameter(description = "用户ID") @PathVariable Long userId) {
        return userAccountService.getUserAccounts(userId);
    }

    @Operation(summary = "充值", description = "用户账户充值")
    @PostMapping("/recharge")
    public Result<String> recharge(@Validated @RequestBody TransactionRequest request) {
        return userAccountService.recharge(request);
    }

    @Operation(summary = "消费", description = "用户账户消费")
    @PostMapping("/consume")
    public Result<String> consume(@Validated @RequestBody TransactionRequest request) {
        return userAccountService.consume(request);
    }

    @Operation(summary = "退款", description = "用户账户退款")
    @PostMapping("/refund")
    public Result<String> refund(@Validated @RequestBody TransactionRequest request) {
        return userAccountService.refund(request);
    }

    @Operation(summary = "冻结金额", description = "冻结账户指定金额")
    @PostMapping("/{accountId}/freeze")
    public Result<Void> freezeAmount(
            @Parameter(description = "账户ID") @PathVariable Long accountId,
            @Parameter(description = "冻结金额") @RequestParam BigDecimal amount) {
        return userAccountService.freezeAmount(accountId, amount);
    }

    @Operation(summary = "解冻金额", description = "解冻账户指定金额")
    @PostMapping("/{accountId}/unfreeze")
    public Result<Void> unfreezeAmount(
            @Parameter(description = "账户ID") @PathVariable Long accountId,
            @Parameter(description = "解冻金额") @RequestParam BigDecimal amount) {
        return userAccountService.unfreezeAmount(accountId, amount);
    }

    @Operation(summary = "申请提现", description = "用户申请提现")
    @PostMapping("/withdrawal/apply")
    public Result<String> applyWithdrawal(@Validated @RequestBody WithdrawalRequest request) {
        return userAccountService.applyWithdrawal(request);
    }

    @Operation(summary = "处理提现", description = "处理用户提现申请")
    @PostMapping("/withdrawal/process")
    public Result<Void> processWithdrawal(
            @Parameter(description = "提现单号") @RequestParam String withdrawalNo,
            @Parameter(description = "处理状态") @RequestParam String status,
            @Parameter(description = "处理备注") @RequestParam String remark) {
        return userAccountService.processWithdrawal(withdrawalNo, status, remark);
    }

    @Operation(summary = "查询账户余额", description = "查询指定账户的余额")
    @GetMapping("/{accountId}/balance")
    public Result<BigDecimal> getAccountBalance(@Parameter(description = "账户ID") @PathVariable Long accountId) {
        return userAccountService.getAccountBalance(accountId);
    }

    @Operation(summary = "更新账户状态", description = "更新账户状态")
    @PutMapping("/{accountId}/status")
    public Result<Void> updateAccountStatus(
            @Parameter(description = "账户ID") @PathVariable Long accountId,
            @Parameter(description = "账户状态") @RequestParam String status) {
        return userAccountService.updateAccountStatus(accountId, status);
    }

    @Operation(summary = "账户统计信息", description = "获取账户统计信息")
    @GetMapping("/statistics")
    public Result<Object> getAccountStatistics() {
        return userAccountService.getAccountStatistics();
    }
}